<template>
	<view class="container">
		<u--form ref="queryForm" :model="query" label-width="120rpx">
		  <u-form-item label="料架码">
		    <u--input 
		    	placeholder="请输入/扫描" 
		    	v-model.trim="query.rackCode" 
		    	suffixIcon="scan" 
		    	suffixIconStyle="font-size: 68rpx"
		    	style="flex: 1;"	
					@confirm="checkIsLightDown"
		    >
		    </u--input>
		  </u-form-item>
		</u--form>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import { mapState } from 'vuex'
	import { checkIsLightAPI, displayColorAPI, unBindCommonMaterialAPI } from '@/api/work/outbound.js'
	export default {
		data() {
			return {
				query: {
					rackCode: ''
				},
				type: ''
			};
		},
		computed: {
			...mapState(['macAddress'])
		},
		methods: {
			async checkRackCode() {
				const submitForm = {
					macUrl: this.macAddress,
					rackCode: this.query.rackCode
				}
				const res = await unBindCommonMaterialAPI(submitForm)
				if (res.code !== 200) {
					return this.$refs.uToast.show({
						type: 'warning',
						message: res.message
					})
				} else {
					uni.navigateTo({
						url: `/pages/work/outbound/shelfPage/commonShelf?rackCode=${this.query.rackCode}`
					})
				}
			},
			async checkIsLightDown() {
				const submitForm = {
					rackCode: this.query.rackCode,
					url: this.macAddress
				}
				const res = await checkIsLightAPI(submitForm)
				console.log(res)
				if (res.code === 200) {
					if (res.data === true) {
							const newSubmitForm = {
								rackCode: this.query.rackCode,
								url: this.macAddress
							}
							// 请求数据
							const result = await displayColorAPI(newSubmitForm)
							if (result.code !== 200) {
								return this.$refs.uToast.show({
									type: 'warning',
									message: result.message
								})
							}
						uni.navigateTo({
							url: `/pages/work/outbound/shelfPage/lightShelf?rackCode=${this.query.rackCode}`
						})
					} else {
						this.checkRackCode()
					}
				} else {
					return this.$refs.uToast.show({
						type: 'warning',
						message: res.message
					})
				}
			}
		}
	}
</script>

<style lang="scss">

</style>
